<?php
class ProofreadSend
{
    private $id;
    private $dataArray;
    private $dbHandle;
    private $logObject;
    private $checkProgrammeProofread;
    private $checkVideoViStatus;

    # set episode/programme status don't delete!
    private $checkEpisodeProgrammeStatus;
	private $sendProofreadResult;

	# append by vanni
	private $audit_log_id;
    private $operationUser;

    public function getProofread($dbHandle, $type, $id, $audit_log_id=0, $send_proofread_result = TRUE)/*{{{*/
    {
		# append by vanni
		$this->audit_log_id =   $audit_log_id;
        $this->operationUser=   CommonUtils::get_user();

        $this->dbHandle =   $dbHandle;
        $this->id       =   $id;
        $this->logObject=   TRUE;
        $this->checkVideoViStatus           =   TRUE;
        $this->checkProgrammeProofread      =   TRUE;
        $this->checkEpisodeProgrammeStatus  =   TRUE;
		$this->sendProofreadResult = $send_proofread_result;

        $proof_type = $type = strtolower($type);
        if ('episode' == $type || 'programme' == $type) {
			$type = $type.'Send';
		}

        $this->$type($id);
		
        if ($this->checkEpisodeProgrammeStatus  &&  $this->checkProgrammeProofread  &&  $this->checkVideoViStatus)
		{
			try{
				$result = $this->sendToMq();
				return $result;
			}catch(Stomp_Exception $e){
				mail(CommonUtils::getAdminMail(),"Proofread($type,$id,$audit_log_id):MQ Server Error, At ".trim(`hostname`) , print_r($e,1));
                Model::updateBySql('update audit_log set action=:action where id=:id',array(':action'=>AC::LOG_OP_PUBLISH_FAILED,':id'=>$audit_log_id));
				return false;
			}
        }
        elseif (!$this->checkEpisodeProgrammeStatus)
        {
            # episode or programme is deleted!
            return  '0203';
        }
        elseif (!$this->checkProgrammeProofread)
        {
            # episode -> programme don't proofread!
            return  '0201';
        }
        elseif (!$this->checkVideoViStatus)
        {
            # episode, programme , check video vi_status is not't success!
            return  '0202';
        }
        else
        {
            return  FALSE;
        }
    }/*}}}*/

    public function getProofreadData($dbHandle, $type, $id)/*{{{*/
    {
        $this->dbHandle =   $dbHandle;
        $this->id       =   $id;
        $this->logObject=   FALSE;
        $this->checkVideoViStatus           =   TRUE;
        $this->checkProgrammeProofread      =   TRUE;
        $this->checkEpisodeProgrammeStatus  =   TRUE;

        $type   =   strtolower($type);
        if ('episode'   ==  $type   ||  'programme' ==  $type)  $type   =   $type.'Send';

        # init $this->dataArray
        $this->dataArray = array(
			'tag'                       =>  array(),
			'folder'                    =>  array(),
			'folder-language'           =>  array(),
			'folder_item'               =>  array(),
			'folder_item-language'      =>  array(),
			'image'                     =>  array(),
			'image-language'            =>  array(),
            'image_folder-config'       =>  array(),
			'video'                     =>  array(),
			'video-language'            =>  array(),
			'video_export'              =>  array(),
			'cast'                      =>  array(),
			'cast-language'             =>  array(),
		);
        $this->$type($id);
        return  $this->dataArray;
    }/*}}}*/

    private function programmeSend($id)/*{{{*/
    {
        $this->dataArray = array(
			'programme'                 =>  array(),
			'programme-language'        =>  array(),
			'programme-tvbcom_category' =>  array(),
			'programme_cpl'             =>  array(),
			'programme_clone'           =>  array(),
			'tag'                       =>  array(),
			'folder'                    =>  array(),
			'folder-language'           =>  array(),
			'folder_item'               =>  array(),
			'folder_item-language'      =>  array(),
			'image'                     =>  array(),
			'image-language'            =>  array(),
			'image_folder-config'       =>  array(),
			'video'                     =>  array(),
			'video-language'            =>  array(),
			'video_export'              =>  array(),
			'cast'                      =>  array(),
			'cast-language'             =>  array(),
            'cast_dictionary'           =>  array(),
			'episode'                   =>  array(),
			'episode-language'          =>  array(),
			'episode-video'             =>  array(),
			#'mis_pdes'                  =>  array(), // append by vanni
		);

		$this->proof_type = 'programme';
		$this->programme_id = $id;
		$this->episode_id = 0;
        $this->programme($this->id);
		
        if (!$this->checkEpisodeProgrammeStatus ||  !$this->checkVideoViStatus) return NULL;

        if (!empty($this->dataArray['programme'][0]['folders']))
        {
            $this->getImage($this->dataArray['programme'][0]['folders']);
            $this->getImageFolder($this->dataArray['programme'][0]['folders']);
            $this->getVideo($this->dataArray['programme'][0]['folders']);
        }
        $this->getCast('programme');

        #cast_dictionary:image
        $this->getCastDictionaryByImage();

        #cast_dictionary:video
        $this->getCastDictionaryByVideo();

        #cast_dictionary:chapter
        $this->getCastDictionaryByChapter();

        #tag:image
        $this->getTagByImage();

        #tag:video
        $this->getTagByVideo();

    }/*}}}*/

    private function episodeSend($id)/*{{{*/
    {
        $this->dataArray = array(
			'episode'                   =>  array(),
			'episode-language'          =>  array(),
			'tag'                       =>  array(),
			'folder'                    =>  array(),
			'folder-language'           =>  array(),
			'folder_item'               =>  array(),
			'folder_item-language'      =>  array(),
			'image'                     =>  array(),
			'image-language'            =>  array(),
			'image_folder-config'       =>  array(),
			'info_address'              =>  array(),
			'info_award'                =>  array(),
			'info_biography'            =>  array(),
			'info_general_information'  =>  array(),
			'info_pageant'				=>  array(),
			'info_product'              =>  array(),
			'info_q_a'                  =>  array(),
			'info_recipe'               =>  array(),
			'info_song'                 =>  array(),
			'cast'                      =>  array(),
			'cast-language'             =>  array(),
            'cast_dictionary'           =>  array(),
			#'mis_pdes'                  =>  array(), // append by vanni
		);
        $this->episode($this->id);
        if (!$this->checkEpisodeProgrammeStatus ||  !$this->checkVideoViStatus) return  NULL;
        if ($this->logObject    &&  !$this->checkProgrammeProofread)            return  NULL;

        if (!empty($this->dataArray['episode'][0]['folders']))
        {
            $this->getImage($this->dataArray['episode'][0]['folders']);
            $this->getImageFolder($this->dataArray['episode'][0]['folders']);
        }
        $this->info($this->id);
        $this->getCast('episode');

        #cast_dictionary
        $this->getCastDictionaryByImage();

        #tag
        $this->getTagByImage();

		$this->proof_type = 'episode';
		$this->programme_id = $this->dataArray['episode'][0]['programme_id'];
		$this->episode_id = $this->id;
    }/*}}}*/

    private function programme($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data   =   $tmpData    =   NULL;
        $logId  =   array();

        # get programme /*{{{*/
        $proofread  =   new programme();
        $sqlArray   =   $proofread->getProofread($id);
        foreach ($sqlArray  as  $tableName  =>  $sql)
        {
            $data[$tableName]   =   CommonUtils::exec_sql($this->dbHandle, $sql);
        }/*}}}*/

        # check self STATUS , if deleted ,the don't Proofread it!/*{{{*/
        if ((empty($data['programme'][0]['status']) ||  AC::STATUS_DELETED  ==  $data['programme'][0]['status']))
        {
            $this->checkEpisodeProgrammeStatus  =   FALSE;
            return  NULL;
        }/*}}}*/

		# check video  這邊的條件是：Video沒有被刪除，並且沒有準備好的Video，是不能Proofread的 /*{{{*/
        if ($this->logObject)
        {
            $checkVideo =   $this->checkVideoStatus($data['programme'][0]['folders']);
            if (!empty($checkVideo))
            {
                $this->checkVideoViStatus   =   FALSE;
                return  NULL;
            }
        }/*}}}*/

        # save programme data to $this->dataArray /*{{{*/
        foreach ($data  as  $tableName  =>  $tmpData)
        {
            if (empty($tmpData) ||  !is_array($tmpData))
            {
                if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                continue;
            }
            foreach ($tmpData   as  $dataValue)
            {
                $this->dataArray[$tableName][]  =   $dataValue;
            }
        }/*}}}*/

        # get tags /*{{{*/
        if (empty($this->dataArray['programme'][0]['tags']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $this->dataArray['programme'][0]['tags']);
            $logId['tag']   =   $idArray;
        }
        $proofread      =   new tag();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName]  =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]    =   $dataValue;
                }
            }
        }/*}}}*/

        # get folders /*{{{*/
        if (empty($data['programme'][0]['folders']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['programme'][0]['folders']);
            $logId['folder']=   $idArray;
        }
        $proofread      =   new folder();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName]  =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]    =   $dataValue;
                }
            }
        }/*}}}*/

		# get mis_pdes data 
		#$mis_pdes_data = mis_pdes::getRelation(array('pdes_pid'=>$id));
		#if($mis_pdes_data){ $this->dataArray['mis_pdes'] = $mis_pdes_data; }
    }/*}}}*/

    private function episode($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data       =   $tmpData    =   NULL;
        $logId      =   array();

        # get episode /*{{{*/
        $proofread = new episode();
        $sqlArray = $proofread->getProofread($id);
        foreach ($sqlArray as $tableName=>$sql) {
			if ($tableName == 'episode-video') {
				continue;
			}
            $data[$tableName] = CommonUtils::exec_sql($this->dbHandle, $sql);
        }
		/*}}}*/

        # check self STATUS , if deleted ,the don't Proofread it!/*{{{*/
        if ((empty($data['episode'][0]['status'])   ||  AC::STATUS_DELETED  ==  $data['episode'][0]['status']))
        {
            $this->checkEpisodeProgrammeStatus  =   FALSE;
            return  NULL;
        }/*}}}*/

        # get programme/*{{{*/
        if ($this->logObject && !empty($data['episode'][0]['programme_id'])) {
            $publish_log_detail = publish_log::getNonPremiumPublishLog('programme' , $data['episode'][0]['programme_id']);
            if (empty($publish_log_detail) || $publish_log_detail->status == AC::STATUS_UNPUBLISH) {
                $this->checkProgrammeProofread = FALSE;
                return  NULL;
            }
        }/*}}}*/

        # save episode data to $this->dataArray /*{{{*/
        foreach ($data  as  $tableName  =>  $tmpData)
        {
            if (empty($tmpData) ||  !is_array($tmpData))
            {
                if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                continue;
            }
            foreach ($tmpData   as  $dataValue)
            {
                $this->dataArray[$tableName][]  =   $dataValue;
            }
        }/*}}}*/

        # get tags /*{{{*/
        if (empty($data['episode'][0]['tags']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['episode'][0]['tags']);
            $logId['tag']   =   $idArray;
        }
        $proofread  =   new tag();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]  =   $dataValue;
                }
            }
        }
        /*}}}*/

        # get folders /*{{{*/
        if (empty($data['episode'][0]['folders']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['episode'][0]['folders']);
            $logId['folder']=   $idArray;
        }
        $proofread  =   new folder();
        foreach ($idArray   as  $ID)
        {
			$sqlArray   =   $proofread->getProofread($ID);
			if(is_array($sqlArray))
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]  =   $dataValue;
                }
            }
        }
        /*}}}*/
		# get mis_pdes data 
		#$mis_pdes_data = mis_pdes::getRelation(array('pdes_pid'=>$data['episode'][0]['programme_id'],'pdes_eid'=>$id));
		#if($mis_pdes_data){ $this->dataArray['mis_pdes'] = $mis_pdes_data; }
    }/*}}}*/

    private function video($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data       =   $tmpData    =   NULL;
        $logId      =   array();

        # get video /*{{{*/
        $proofread  =   new video();
        $sqlArray   =   $proofread->getProofread($id);
        foreach ($sqlArray  as  $tableName  =>  $sql)
        {
            $data[$tableName]   =   CommonUtils::exec_sql($this->dbHandle, $sql);
        }/*}}}*/

		# add by vanni at 2011-05-10 for bug 6587 
		if(empty($data['video'][0])) return;
		
        # check self STATUS , if deleted ,then don't Proofread it!/*{{{*/
        if ((empty($data['video'][0]['status']) || AC::STATUS_DELETED == $data['video'][0]['status'])) {
            /*
             * delete the relation of episode-video from $this->dataArray
             * because the video is not useful, so the relation no need send to MQ
             * append by donald 2009-09-22 
             * for bug 739
             */
            foreach ($this->dataArray['episode-video'] as $key=>$row) {
                if ($row['video_id'] == $id) {
                    unset($this->dataArray['episode-video'][$key]);
                }
            }

            return  NULL;
        }/*}}}*/

        # save video data to $this->dataArray /*{{{*/
        foreach ($data as $tableName=>$tmpData) {
            if (empty($tmpData) ||  !is_array($tmpData)){
                if (!isset($this->dataArray[$tableName])) {
					$this->dataArray[$tableName] = array();
				}
				
                continue;
            }
			
            foreach ($tmpData as $dataValue) {
                $this->dataArray[$tableName][] = $dataValue;
            }
        }/*}}}*/

        # get cast by video
        if (!empty($data['video'][0]['casts']))
        {
            $this->getCast('video', $data['video'][0]['casts']);
        }

        # get chapter /*{{{*/
        $chapter    =   new chapter();
        $chapter    =   $chapter->getChapterIDByVideoID($id);
        if (empty($chapter))
        {
            $idArray=   array('-1');
        }
        else
        {
            $idArray=   explode(',', $chapter);
        }

        foreach ($idArray   as  $chapterID)
        {
            $this->chapter($chapterID);
        }/*}}}*/

        # get episode from episode-video /*{{{*/
        # just for programme
        if (!empty($this->dataArray['programme'][0]['id'])) {
            if (empty($data['episode-video'][0]['episode_id'])) {
                $idArray = array(array('episode_id' => '-1'));
            } else {
                $idArray = $data['episode-video'];
            }
        
            $proofread = new episode();
            foreach ($idArray as $ID) {
                $sqlArray = $proofread->getProofread($ID['episode_id']);
                foreach ($sqlArray as $tableName => $sql) {
                    /*
                     * data of episode-video find by episode_id no need send to MQ
					 * bug 793
                     */
                    if ($tableName == 'episode-video') {
                        continue;
                    }

                    $tmpData = CommonUtils::exec_sql($this->dbHandle, $sql);
                    if (empty($tmpData) || !is_array($tmpData)) {
                        if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName] = array();
                        continue;
                    }

                    foreach ($tmpData as $dataValue) {
						if ($tableName == 'episode') {
							$dataValue['status'] = AC::STATUS_HIBERNATE;    
						}
						
                        $this->dataArray[$tableName][] = $dataValue;
                    }
                }
            }
        }/*}}}*/

        # get tags /*{{{*/
        if (empty($data['video'][0]['tags']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['video'][0]['tags']);
            $logId['tag']   =   $idArray;
        }
        $proofread  =   new tag();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName]  =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]    =   $dataValue;
                }
            }
        }
        /*}}}*/
    }/*}}}*/

    private function image($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data       =   $tmpData    =   NULL;

        # get image /*{{{*/
        $proofread  =   new image();
        $sqlArray   =   $proofread->getProofread($id);
        foreach ($sqlArray  as  $tableName  =>  $sql)
        {
            $data[$tableName]   =   CommonUtils::exec_sql($this->dbHandle, $sql);
        }/*}}}*/

        # check self STATUS , if deleted ,the don't Proofread it!/*{{{*/
        if (empty($data['image'][0]['status'])  ||  AC::STATUS_DELETED  ==  $data['image'][0]['status'])
        {
            return  NULL;
        }/*}}}*/

        # save image data to $this->dataArray /*{{{*/
        foreach ($data  as  $tableName  =>  $tmpData)
        {
            if (empty($tmpData) ||  !is_array($tmpData))
            {
                if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                continue;
            }
            foreach ($tmpData   as  $dataValue)
            {
                $this->dataArray[$tableName][]  =   $dataValue;
            }
        }/*}}}*/
    }/*}}}*/

    private function info($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data       =   $tmpData    =   NULL;
        $logId      =   array();

        # get 8 infos /*{{{*/
        $proofread  =   new info();
        $sqlArray   =   $proofread->getProofread($id);
        foreach ($sqlArray  as  $tableName  =>  $sql)
        {
            $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
            $data[$tableName]   =   $tmpData;
            $logId[$tableName]  =   array();

            if (empty($tmpData) ||  !is_array($tmpData))
            {
                if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                continue;
            }
            foreach ($tmpData   as  $dataValue)
            {
                $this->dataArray[$tableName][]          =   $dataValue;
                $logId[$tableName][$dataValue['id']]    =   $dataValue['id'];
            }
        }/*}}}*/
    }/*}}}*/

    private function cast($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data       =   $tmpData    =   NULL;

        # get cast /*{{{*/
        $proofread  =   new cast();
        $sqlArray   =   $proofread->getProofread($id);
        foreach ($sqlArray  as  $tableName  =>  $sql)
        {
            $data[$tableName]   =   CommonUtils::exec_sql($this->dbHandle, $sql);
        }/*}}}*/

        # check self STATUS , if deleted ,the don't Proofread it!/*{{{*/
        if ((empty($data['cast'][0]['status'])  ||  AC::STATUS_DELETED  ==  $data['cast'][0]['status']))
        {
            return  NULL;
        }/*}}}*/

        # save cast data to $this->dataArray /*{{{*/
        foreach ($data  as  $tableName  =>  $tmpData)
        {
            if (empty($tmpData) ||  !is_array($tmpData))
            {
                if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                continue;
            }
            foreach ($tmpData   as  $dataValue)
            {
                $this->dataArray[$tableName][]  =   $dataValue;
            }
        }/*}}}*/

        # get tags /*{{{*/
        if (empty($data['cast'][0]['tags']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['cast'][0]['tags']);
            $logId['tag']   =   $idArray;
        }
        $proofread  =   new tag();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName]  =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]    =   $dataValue;
                }
            }
        }
        /*}}}*/

        # get folders /*{{{*/
        if (empty($data['cast'][0]['folders']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['cast'][0]['folders']);
            $logId['folder']=   $idArray;

            # deal with cast image & video
            $this->getImage($data['cast'][0]['folders']);
            $this->getImageFolder($data['cast'][0]['folders']);
            $this->getVideo($data['cast'][0]['folders']);
        }
        $proofread  =   new folder();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName]  =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]    =   $dataValue;
                }
            }
        }
        /*}}}*/
    }/*}}}*/

    private function chapter($id)/*{{{*/
    {
        if (empty($id)) return  NULL;
        $data       =   $tmpData    =   NULL;
        $logId      =   array();

        # get chapter 
        $proofread  =   new chapter();
        $sqlArray   =   $proofread->getProofread($id);
        foreach ($sqlArray  as  $tableName  =>  $sql)
        {
            $data[$tableName]   =   CommonUtils::exec_sql($this->dbHandle, $sql);
        }

        # check self STATUS , if deleted ,the don't Proofread it!
        if ((empty($data['chapter'][0]['status'])   ||  AC::STATUS_DELETED  ==  $data['chapter'][0]['status']))
        {
            return  NULL;
        }

        # save chapter data to $this->dataArray 
        foreach ($data  as  $tableName  =>  $tmpData)
        {
            if (empty($tmpData) ||  !is_array($tmpData))
            {
                if (!isset($this->dataArray[$tableName]))   $this->dataArray[$tableName]    =   array();
                continue;
            }
            foreach ($tmpData   as  $dataValue)
            {
                $this->dataArray[$tableName][]  =   $dataValue;
            }
        }

        # get tags /*{{{*/
        if (empty($data['chapter'][0]['tags']))
        {
            $idArray    =   array('-1');
        }
        else
        {
            $idArray        =   explode(',', $data['chapter'][0]['tags']);
            $logId['tag']   =   $idArray;
        }
        $proofread  =   new tag();
        foreach ($idArray   as  $ID)
        {
            $sqlArray   =   $proofread->getProofread($ID);
            foreach ($sqlArray  as  $tableName  =>  $sql)
            {
                $tmpData            =   CommonUtils::exec_sql($this->dbHandle, $sql);
                if (empty($tmpData) ||  !is_array($tmpData))
                {
                    if (!isset($this->dataArray[$tableName])) $this->dataArray[$tableName]  =   array();
                    continue;
                }
                foreach ($tmpData   as  $dataValue)
                {
                    $this->dataArray[$tableName][]    =   $dataValue;
                }
            }
        }
        /*}}}*/
    }/*}}}*/

    /**
     * get image by folders
     * @param   $ids    String  folder ids
     */
    private function getImage($ids)/*{{{*/
    {
        $idArray    =   folder::getItemByFolder($ids, 'image');

        if (!empty($idArray) && is_array($idArray))
        {
            foreach ($idArray   as  $imageID)
            {
                $this->image($imageID);
            }
        }
    }/*}}}*/

    /**
     * get image_folder-config
     * @param   $ids    String  folder ids
     */
    private function getImageFolder($ids)/*{{{*/
    {
        if (empty($ids))    return  NULL;

        $sql=   '
            SELECT
                *
            FROM
                `image_folder-config`
            WHERE
                folder_id IN ('. $ids. ')
            ';

        $result =   CommonUtils::exec_sql($this->dbHandle, $sql);
        if (!empty($result[0]['folder_id']))
        {
            foreach ($result    as  $value)
            {
                $this->dataArray['image_folder-config'][]   =   $value;
            }
        }
    }/*}}}*/

    /**
     * get cast for episode, programme, video
     * @param   String  $type   :   episode, programme, video
     */
    private function getCast($type, $castJson = NULL)/*{{{*/
    {
        if (empty($castJson))
        {
            if (empty($this->dataArray[$type][0]['casts'])) return  array();
            $castJson   =   json_decode($this->dataArray[$type][0]['casts'], 1);
        }
        else
        {
            $castJson   =   json_decode($castJson);
        }

        if (is_array($castJson))
        {
            foreach ($castJson  as  $castRole   =>  $castValues)
            {
                if (is_array($castValues))
                {
                    foreach ($castValues    as  $cast)
                    {
                        if (empty($cast['cast_id']))    continue;
                        $this->cast($cast['cast_id']);
                    }
                }
            }
        }
    }/*}}}*/

    private function getCastDictionaryByImage()/*{{{*/
    {
        if (!empty($this->dataArray['image'])) {
            $casts = array();
            foreach ($this->dataArray['image'] as $row) {
                if (!empty($row['casts']) && !is_array(json_decode($row['casts'],TRUE))) {
                    $casts = array_merge($casts, explode(',', $row['casts']));
                }
            }

            if (!empty($casts)) {
               	$this->getCastDictionary(implode(',', $casts));
            }
        }
    }/*}}}*/

    private function getCastDictionaryByVideo()/*{{{*/
    {
        if (!empty($this->dataArray['video'])) {
            $casts = array();
            foreach ($this->dataArray['video'] as $row) {
                if (!empty($row['casts']) && !is_array(json_decode($row['casts'],TRUE))) {
                    $casts = array_merge($casts, explode(',', $row['casts']));
                }
            }

            if (!empty($casts)) {
               	$this->getCastDictionary(implode(',', $casts));
            }
        }
    }/*}}}*/

    private function getCastDictionaryByChapter()/*{{{*/
    {
        if (!empty($this->dataArray['chapter'])) {
            $casts = array();
            foreach ($this->dataArray['chapter'] as $row) {
                if (!empty($row['casts']) && !is_array(json_decode($row['casts'],TRUE))) {
                    $casts = array_merge($casts, explode(',', $row['casts']));
                }
            }

            if (!empty($casts)) {
               	$this->getCastDictionary(implode(',', $casts));
            }
        }
    }/*}}}*/

    private function getCastDictionary($id)/*{{{*/
    {
        if (empty($id)) {
            return FALSE; 
        }

        $sql = "SELECT * FROM `cast_dictionary`
            WHERE `id` IN ( " . $id . ")";
        $result = CommonUtils::exec_sql($this->dbHandle, $sql);
        if (!empty($result)) {
            foreach ($result as $row) {
                $this->dataArray['cast_dictionary'][] = $row;
            }
        }
    }/*}}}*/

    private function getTagByImage()/*{{{*/
    {
        $images = $this->dataArray['image'];
        if (!empty($images)) {
            foreach ($images as $image) {
                if (!empty($image['tags'])) {
                    $this->getTag($image['tags']);
                }
            }
        }
    }/*}}}*/

    private function getTagByVideo()/*{{{*/
    {
        $videos = $this->dataArray['video'];
        if (!empty($videos)) {
            foreach ($videos as $video) {
                if (!empty($video['tags'])) {
                    $this->getTag($video['tags']);
                }
            }
        }
    }/*}}}*/

    private function getTag($id) /*{{{*/
    {
        if (!empty($id)) { 
            $sql = "SELECT * FROM `tag` WHERE `id` IN ( " . $id . ")";
            $result = CommonUtils::exec_sql($this->dbHandle, $sql);
            if (!empty($result)) {
                foreach ($result as $row) {
                    $this->dataArray['tag'][] = $row;
                }
            }
        }
    }/*}}}*/

    /**
     * get video by folders
     * @param   $ids    String  folder ids
     */
    private function getVideo($ids)/*{{{*/
    {
        $idArray    =   folder::getItemByFolder($ids, 'video');

        if (!empty($idArray) && is_array($idArray))
        {
            foreach ($idArray   as  $videoID)
            {
                $this->video($videoID);
            }
        }
    }/*}}}*/

    private function checkVideoStatus($folders)/*{{{*/
    {
        if (empty($folders))    $folders    =   '""'; 

		/** Check CatchUP Folder Only.Add by vanni at 2011-05-10 for bug 6587 */
        $sql=   '
            SELECT
                fi.item_id      AS  itemID
            FROM
                folder      AS  f,
                folder_item AS  fi,
                video       AS  v
            WHERE
                f.id = fi.folder_id
                AND v.id = fi.item_id
                AND v.vi_status <> "'. AC::VIDEO_STATUS_UPLOADED. '"
                AND v.status <>"'. AC::STATUS_DELETED. '"
                AND f.folder_item_type_id = "'.AC::FOLDER_TYPE_VIDEO. '"
				AND f.folder_type_id = "'.AC::VIDEO_CATCHUP.'"
                AND f.id IN ('. $folders .')
            Limit 1
            ';
        $result =   CommonUtils::exec_sql($this->dbHandle, $sql);
        return  empty($result[0]['itemID']) ?   NULL    :   $result;
    }/*}}}*/


    private function sendToMq()/*{{{*/
    {
        if (empty($this->dataArray) ||  !is_array($this->dataArray))    return  NULL;
		$config = ConfigReaderFactory::getInstance()->getConfigReader(AC::CONFIG_MQ)->getConfig();
		$consumerConfig = $config->xpath("/config/consumer[@name='ProofreadSend']");

		$broker = (string) $consumerConfig[0]->broker;
		$queue  = (string) $consumerConfig[0]->queue;

		# append by vanni
		$this->dataArray['proof_type'] = $this->proof_type;
		$this->dataArray['programme_id'] = $this->programme_id;
		$this->dataArray['episode_id'] = $this->episode_id;
		$this->dataArray['audit_log_id']=$this->audit_log_id;
		$this->dataArray['operationUser']=$this->operationUser;
		$this->dataArray['sendProofreadResult'] = $this->sendProofreadResult;

		return MessageHelper::sendMessage($broker, $queue, $this->dataArray);
	}/*}}}*/
}
?>
